using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Pervote.Core;
using Pervote.Core.Logic;

public partial class Controls_ShortPollView : DataValidatingUserControl
{
    private Poll poll;
    public Poll Poll
    {
        get { return poll; }
        set
        {
            poll = value;
        }
    }

    public void LoadAny(List<Poll> polls)
    {
        PollManager pollManager = new PollManager();
        if (polls.Count > 0)
        {
            MembershipUser user = Membership.GetUser();
            string userName = (user != null) ? user.UserName : null;
            ErrorInfo error = new ErrorInfo();
            Poll poll = pollManager.GetPollView(polls[0].Id, userName, error);
            LoadPoll(poll);
        }
    }

    public void LoadPoll(Poll poll)
    {
        this.poll = poll;
        pollItemsRepeater.DataSource = poll.PollItems;
        pollItemsRepeater.DataBind();
        SetDataValid();
    }


    protected string GetLogoCssClass(string fqCatName)
    {
        //get the root category
        string root = fqCatName.Substring(1, fqCatName.IndexOf('/', 1) != -1 ? fqCatName.IndexOf('/', 1) - 1 : fqCatName.Length - 1);
        if (root != "Music" && root != "Politics" && root != "Sports" && root != "Movies" && root != "Fun" && root != "Technology")
            return "catLogo-General";
        return "catLogo-" + root;    
    }

    protected string GetFormattedComment(PollItemComment comment)
    {
        if (String.IsNullOrEmpty(comment.Text)) return "";
        else
        {
            string linkText;
            if (comment.Link == null || comment.Link.Trim() == "") linkText = "";
            else linkText = "<a href=\"" + comment.Link + "\">[More..]</a>";
            
            string ratingLinkText = (comment.UserRated) ? "<img src=\"/Site/Themes/Default/Images/rankUpCommentDim.png\" />&nbsp;<img src=\"/Site/Themes/Default/Images/rankDownCommentDim.png\" />" : "<span class=\"linklessLink\"><img src=\"/Site/Themes/Default/Images/rankUpComment.png\" alt=\"Good Comment\" onclick=\"rateComment(" +
                comment.Id + ",1);\" /></span>&nbsp;<span class=\"linklessLink\"><img src=\"/Site/Themes/Default/Images/rankDownComment.png\" alt=\"Bad Comment\" onclick=\"rateComment(" + comment.Id + ",-1);\" /></span>";
            
            string result = "<br /><div class=\"pollItemComment\"><strong><i>" + comment.AddedBy +
                " says: " + "</i></strong>" + comment.Text + "&nbsp;" + linkText + "&nbsp;<span id=\"rateCommentLink" + comment.Id + "\">" + 
                ratingLinkText + "</span><br /></div>";
            
            return result;
        }

    }

    protected string GetVoteLinkString(bool userVoted)
    {
        if (userVoted == true) return "<span class=\"grey\">Voted</span>";
        else
        {
            if (poll.CanVoteAgain) return "<div class=\"voteButton\">Vote</div>";
            else return "";
        }
    }

    public string GetItemVoteData()
    {
        string pollItemData = "";
        if (poll.PollItems.Count < 1) return "";
        foreach (PollItem item in poll.PollItems)
        {
            pollItemData += item.Id + "~" + item.Name + "~" + item.TotalVotes + "~";
        }
        pollItemData = pollItemData.Substring(0, pollItemData.Length - 1); //remove the trailing comma!
        return pollItemData;
    }
}
